package com.thread.threadfirst.javaBase.classLoader;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 类加载器命名空间
 */
public class ClassLoaderNamespace {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        CustomClassLoader classLoader1=new CustomClassLoader("classLoader1");
        classLoader1.setLoadPath("D:\\exercise\\target\\classes\\");

        CustomClassLoader classLoader2=new CustomClassLoader("classLoader2");
        classLoader2.setLoadPath("D:\\exercise\\target\\classes\\");

        Class clazz1=classLoader1.loadClass("com.thread.threadfirst.javaBase.classLoader.Role");
        System.out.println("clazz1类加载器 "+clazz1.getClassLoader());

        Class clazz2=classLoader2.loadClass("com.thread.threadfirst.javaBase.classLoader.Role");
        System.out.println("clazz2类加载器 "+clazz2.getClassLoader());

        System.out.println("clazz1==clazz2 " + (clazz1==clazz2) );//false

        Object role1=clazz1.newInstance();
        Object role2=clazz2.newInstance();

        Method method=clazz1.getMethod("serRole",Object.class);
        method.invoke(role1,role2);//报错：com tuling.smlz.jvm.open.TheSameClassLoadedByDiffClassLoader.Person cannot be cast to com.tuling.smlz.jvm.open.TheSameClassLoadedByDiffClassLoader Person

    }
}
